package com.F05.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.F03.dao.PdPlanningDAO;
import com.F05.actionform.F0503ActionForm;
import com.F05.actionform.F0506ActionForm;
import com.F05.dao.FInvoicingProductionDAO;
import com.F05.info.FInvoicingProductionInfo;
import com.F09.actionform.F0905ActionForm;
import com.F09.dao.FunctionLanguageDAO;
import com.action.BaseAction;
import com.config.Constants;
import com.exception.SelectException;
import com.info.T_S_YHCZXXinfo;
import com.util.CommonTools;
import com.util.SessionUtils;

/**
 * 
 * @file F0503Action.java
 * @author huqy
 * @since 2009-3-15
 * @version 1.0
 */
public class F0503Action extends BaseAction {

	/**
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	Logger log = Logger.getLogger(F0503Action.class);

	/** 操作记录INFO */
	T_S_YHCZXXinfo t_YhczxxInfo = null;
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		F0503ActionForm actionForm = (F0503ActionForm) form;
		FInvoicingProductionDAO dao = new FInvoicingProductionDAO();
		FInvoicingProductionInfo info = new FInvoicingProductionInfo();
		String operationType = actionForm.getOperationType();
		//String strDetailSelf="detailself";
		//String strDetailOut = "detailout";
		String strDetail = "detail";
		String strList = "list";
		String language = SessionUtils.getLocale(request);
		actionForm.setLanguage(language);
		try {
			//操作记录
			t_YhczxxInfo = setYhczxx(request, response, operationType,"F0503");
		
//			if("queryOut".equals(operationType)){
//				initQuery(actionForm,dao,request,response);
//				return mapping.findForward(strDetailOut);
//			}
//			else if("querySelf".equals(operationType)){
//				initQuery(actionForm,dao,request,response);
//				return mapping.findForward(strDetailSelf);
//			}
			if(Constants.AJAX.equals(operationType)){
				initAjax(dao,request,response);
				return null;
			}
			else if("add".equals(operationType)){
				doAdd(actionForm,dao);
				return mapping.findForward(strDetail);	
			}
//			else if("addself".equals(operationType)){
//				doAdd(actionForm,dao);
//				
//				return mapping.findForward(strDetailSelf);	
//			}
//			else if("addout".equals(operationType)){
//				doAdd(actionForm,dao);
//				
//				return mapping.findForward(strDetailOut);	
//			}
			else if(Constants.SAVE_INSERT.equals(operationType)){
				
				if(doInsert(actionForm,dao)){	
					String url=mapping.findForward("doList").getPath();
		            ActionForward forward=new ActionForward(url+"?operationType=queryList&errorFlag="+actionForm.getErrorFlag());
		    		return forward;
				}else{
					//actionForm.setErrorFlag("主键重复，保存失败!");
					actionForm.setErrorFlag("4");
					return mapping.findForward(strDetail);
//					if(CommonTools.isNullString(actionForm.getManufacturingOrder())){
//						return mapping.findForward(strDetailOut);
//					}else{
//						return mapping.findForward(strDetailSelf);
//					}						
				}
			}
		
			else if(Constants.SAVE_UPDATE.equals(operationType)){
				if(doUpdate(actionForm,dao)){				
					String url=mapping.findForward("doList").getPath();
		            ActionForward forward=new ActionForward(url+"?operationType=queryList&errorFlag="+actionForm.getErrorFlag());
		    		return forward;
				}else{
					 return mapping.findForward(strDetail);
//					if(actionForm.getManufacturingOrder().equals("")){
//						 return mapping.findForward(strDetailOut);
//					}else {
//						 return mapping.findForward(strDetailSelf);
//					}
				}
			}
			else if(Constants.EDIT.equals(operationType)){
				doEdit(actionForm,dao);
				return mapping.findForward(strDetail);
//				if(actionForm.getManufacturingOrder().equals("")){
//					 return mapping.findForward(strDetailOut);
//				}else {
//					 return mapping.findForward(strDetailSelf);
//				}
               
			}
		
			if(Constants.DELETE.equals(operationType)){
				if(doDelete(actionForm,dao)){				
					String url=mapping.findForward("doList").getPath();
		            ActionForward forward=new ActionForward(url+"?operationType=queryList");
		    		return forward;
				}else{
					return mapping.findForward("list");		
				}
			}
	
			else if(Constants.BACK.equals(operationType)){
			
				return mapping.findForward(strList);
			}
			
			/*
			if(strQuery.equals(operationType)){
				
				initQuery(actionForm,dao,request,response);
				return mapping.findForward(strDetail);
			}
			if(strQueryAdvance.equals(operationType)){
				initPreviousAdvance(actionForm,dao,request,response);
				return mapping.findForward(strDetail);
			}
			*/
			else if(Constants.QUERYLIST.equals(operationType)){
				info.setContractNum1(CommonTools.null2String(actionForm.getContractNum1()));
				info.setContractNum2(CommonTools.null2String(actionForm.getContractNum2()));
				info.setInvoiceDate1(CommonTools.null2String(actionForm.getInvoiceDate1()));
				info.setInvoiceDate2(CommonTools.null2String(actionForm.getInvoiceDate2()));
				info.setManufacturingOrder1(CommonTools.null2String(actionForm.getManufacturingOrder1()));
				info.setManufacturingOrder2(CommonTools.null2String(actionForm.getManufacturingOrder2()));
				info.setInvoiceNumber1(CommonTools.null2String(actionForm.getInvoiceNumber1()));
				info.setInvoiceNumber2(CommonTools.null2String(actionForm.getInvoiceNumber2()));
				info.setAdvancedInvoice(CommonTools.null2String(actionForm.getAdvancedInvoice()));
				info.setInvoiceFlag(CommonTools.null2String(actionForm.getInvoiceFlag()));
				doQueryList(actionForm,dao.queryList(info));
				//actionForm.setList(dao.queryList(info));
				return mapping.findForward(strList);
			}
			else if(Constants.INIT.equals(operationType)){
				return mapping.findForward(strList);
			}
			
		} catch (Exception e){
			e.printStackTrace();
		} finally {
			if (dao != null) dao.disConnect();
		}
		return mapping.findForward(strList);
	}
	
	private void doQueryList(F0503ActionForm actionForm,List list){
		if(list!=null&&list.size()>0){
			actionForm.setList(list);
		}
		else{
			actionForm.setList(list);
			actionForm.setErrorFlag("2");
		}	
	}
	
	private void  doEdit(F0503ActionForm actionForm,FInvoicingProductionDAO dao) throws SelectException{
		try {
			
		    Map map = null;
		    List list = new ArrayList();
			list = dao.selectById(actionForm.getId());
			 for(int i = 0; i < list.size();i++){
				map = (Map)list.get(i);	
				actionForm.setId(CommonTools.null2String((String)map.get("id")));
				actionForm.setInvoiceNumber(CommonTools.null2String((String)map.get("invoiceNumber")));
				actionForm.setInvoiceDate(CommonTools.null2String((String)map.get("invoiceDate")));
				actionForm.setCustomersCode(CommonTools.null2String((String)map.get("customersCode")));
				actionForm.setManufacturingOrder(CommonTools.null2String((String)map.get("manufacturingOrder")));
				actionForm.setUnitPrice(CommonTools.null2String((String)map.get("unitPrice")));
				actionForm.setQty(CommonTools.null2String((String)map.get("qty")));
				actionForm.setTotalRbm(CommonTools.null2String((String)map.get("totalRbm")));
				actionForm.setTotalUsd(CommonTools.null2String((String)map.get("totalUsd")));
				actionForm.setTotalEuros(CommonTools.null2String((String)map.get("totalEuros")));
				actionForm.setAdvancedInvoice(CommonTools.null2String((String)map.get("advancedInvoice")));
				actionForm.setInvoiceFlag(CommonTools.null2String((String)map.get("invoiceFlag")));
				actionForm.setContractNum(CommonTools.null2String((String)map.get("contractNum")));
				actionForm.setCustomersName(CommonTools.null2String((String)map.get("customersName")));
				actionForm.setTotalPrice(CommonTools.null2String((String)map.get("totalPrice")));
				actionForm.setTotalRbmOther(CommonTools.null2String((String)map.get("totalRbm")));
				actionForm.setLayOutType("");
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
//	private void initQuery(F0503ActionForm actionForm,FInvoicingProductionDAO dao,HttpServletRequest request, HttpServletResponse response){
//		
//		String responseXML = "";
//		try{
//			response.setContentType("text/xml");
//			response.setCharacterEncoding("GBK");
//			PrintWriter out = response.getWriter();
//			responseXML = dao.initQuery(actionForm.getContractNum());
//			out.write(responseXML);
//			out.flush();
//			out.close();
//		}catch (Exception e){
//			e.printStackTrace();
//		}
//	}

	private void initAjax(FInvoicingProductionDAO dao,HttpServletRequest request, HttpServletResponse response){
		
		try{
			response.setContentType("text/xml");
			response.setCharacterEncoding("UTF-8");
			String id = CommonTools.trim(request.getParameter("id"));
			String responseXML = "";
			if(id.equals("manufacturingOrder")){
				String manufacturingOrder = CommonTools.trim(request.getParameter("manufacturingOrder"));
				responseXML = dao.initQuery(manufacturingOrder);	
			}
			
			log.info(responseXML.toString());
			response.getWriter().write(responseXML.toString());
									
		}catch (Exception e){
			e.printStackTrace();
		}
	}
	private boolean doDelete(F0503ActionForm actionForm,FInvoicingProductionDAO dao){
		boolean flag=false;
		try {
			String id=CommonTools.null2String(actionForm.getId());
			flag=dao.delete(id, actionForm);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	private void doAdd(F0503ActionForm actionForm,FInvoicingProductionDAO dao){
		try {
			
			actionForm.setLayOutType("add");
			actionForm.setContractNum("");
			actionForm.setInvoiceNumber("");
			actionForm.setCustomersCode("");
			actionForm.setCustomersName("");
			actionForm.setManufacturingOrder("");
			actionForm.setUnitPrice("");
			actionForm.setInvoiceDate("");
			actionForm.setQty("");
			actionForm.setTotalRbm("");
			actionForm.setTotalUsd("");
			actionForm.setTotalEuros("");
			actionForm.setAdvancedInvoice("");

		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	private boolean doInsert(F0503ActionForm actionForm,FInvoicingProductionDAO dao){
		boolean flag=false;
		FInvoicingProductionInfo info=new FInvoicingProductionInfo();
		try {
	
//			if(!CommonTools.isNullString(actionForm.getManufacturingOrder())){
//				info.setInvoiceFlag("0");
//			}else{
//				info.setInvoiceFlag("1");
//			}
			info.setContractNum(CommonTools.null2String(actionForm.getContractNum()));
			info.setInvoiceNumber(CommonTools.null2String(actionForm.getInvoiceNumber()));
			info.setCustomerCode(CommonTools.null2String(actionForm.getCustomersCode()));
			info.setManufacturingOrder(CommonTools.null2String(actionForm.getManufacturingOrder()));
			info.setUnitPrice(CommonTools.null2String(actionForm.getUnitPrice()));
			info.setQty(CommonTools.null2String(actionForm.getQty()));
			info.setTotalRbm(CommonTools.null2String(actionForm.getTotalRbm()));
			info.setTotalUsd(CommonTools.null2String(actionForm.getTotalUsd()));
			info.setTotalEuros(CommonTools.null2String(actionForm.getTotalEuros()));
			info.setInvoiceDate(CommonTools.null2String(actionForm.getInvoiceDate()));
			info.setAdvancedInvoice(CommonTools.null2String(actionForm.getAdvancedInvoice()));
			info.setInvoiceFlag(CommonTools.null2String(actionForm.getInvoiceFlag()));
			
			flag=dao.insert(info,actionForm);	
			
		} catch (Exception e) {
			e.printStackTrace();
		}	
		return flag;
	}

	private boolean doUpdate(F0503ActionForm actionForm,FInvoicingProductionDAO dao){
		boolean flag=false;
		try {
			actionForm.setErrorFlag("");
			FInvoicingProductionInfo info=new FInvoicingProductionInfo();
			info.setId(CommonTools.null2String(actionForm.getId()));
			info.setContractNum(CommonTools.null2String(actionForm.getContractNum()));
			info.setInvoiceNumber(CommonTools.null2String(actionForm.getInvoiceNumber()));
			info.setCustomerCode(CommonTools.null2String(actionForm.getCustomersCode()));
			info.setManufacturingOrder(CommonTools.null2String(actionForm.getManufacturingOrder()));
			info.setUnitPrice(CommonTools.null2String(actionForm.getUnitPrice()));
			info.setQty(CommonTools.null2String(actionForm.getQty()));
			info.setTotalRbm(CommonTools.null2String(actionForm.getTotalRbm()));
			info.setTotalUsd(CommonTools.null2String(actionForm.getTotalUsd()));
			info.setTotalEuros(CommonTools.null2String(actionForm.getTotalEuros()));
			info.setInvoiceDate(CommonTools.null2String(actionForm.getInvoiceDate()));
			info.setAdvancedInvoice(CommonTools.null2String(actionForm.getAdvancedInvoice()));
			
			flag=dao.update(info,actionForm);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;	
	}
	
}
